package GUI;
import java.awt.Rectangle;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Product;

/**
 * This class use for show and update product details.
 * @author Ameer
 * @author waleed
 * @author bshara
 * @author kamil
 *
 */
public class ProductForm extends JPanel
{
	private JLabel pNameLabel = null;
	private JLabel pPriceLabel = null;
	private JLabel pDescLabel = null;
	private JLabel pCNumlLabel = null;
	private JTextField pNameField = null;
	private JTextField pPriceTextField = null;
	private TextArea pDescTextArea = null;
	private JTextField pCnumTextField = null;
	private JButton saveButton = null;
	private JButton cancelButton = null;
	private JButton updateButton = null;
	private JButton removeButton = null;
	Product product = null;
		
	/**
	 * This is the default constructor
	 */
	public ProductForm() 
	{
		super();
		initialize();	
	}
	
	/**
	 * This method initializes ProductForm
	 */
	private void initialize() 
	{
		pCNumlLabel = new JLabel();
		pCNumlLabel.setBounds(new Rectangle(15, 17, 113, 22));
		pCNumlLabel.setText("Category number");
		
		pNameLabel = new JLabel();
		pNameLabel.setBounds(new Rectangle(15, 46, 113, 22));
		pNameLabel.setText("Product name");
		
		pPriceLabel = new JLabel();
		pPriceLabel.setBounds(new Rectangle(15, 74, 113, 22));
		pPriceLabel.setText("Product price");
		
		pDescLabel = new JLabel();
		pDescLabel.setBounds(new Rectangle(15, 105, 113, 22));
		pDescLabel.setText("Product description");
			
		product = new Product();
		
		this.setSize(400, 300);
		this.setLayout(null);
		this.add(pCNumlLabel, null);
		this.add(pNameLabel, null);
		this.add(pPriceLabel, null);
		this.add(pDescLabel, null);
		this.add(getPCnumTextField(), null);
		this.add(getPNameTextField(), null);
		this.add(getPPriceTextField(), null);
		this.add(getPDescpDescTextArea(), null);
		this.add(getcancelButton(),null);
	}
	
	/**
	 * This method initializes pCnumTextField	
	 */
	private JTextField getPCnumTextField()
	{
		if (pCnumTextField == null) 
		{
			pCnumTextField = new JTextField();
			pCnumTextField.setBounds(new Rectangle(133, 17, 127, 25));
		}
		return pCnumTextField;
	}

	/**
	 * This method initializes pNameTextField	
	 */
	private JTextField getPNameTextField()
	{
		if (pNameField == null) 
		{
			pNameField = new JTextField();
			pNameField.setBounds(new Rectangle(133, 46, 127, 25));
		}
		return pNameField;
	}
	
	/**
	 * This method initializes pPriceTextField
	 */
	private JTextField getPPriceTextField()
	{
		if (pPriceTextField == null) 
		{
			pPriceTextField = new JTextField();
			pPriceTextField.setBounds(new Rectangle(133, 74, 127, 25));
		}
		return pPriceTextField;
	}

	/**
	 * This method initializes pDescTextField	
	 */
	private TextArea getPDescpDescTextArea()
	{
		if (pDescTextArea == null) 
		{
			pDescTextArea = new TextArea();
			pDescTextArea.setBounds(new Rectangle(133, 105, 130, 70));			
		}
		return pDescTextArea;
	}
	
	/**
	 * This method initializes SaveButton	
	 */
	private JButton getSaveButton() 
	{
		if (saveButton == null) 
		{
			saveButton = new JButton();
			saveButton.setBounds(new Rectangle(150, 200, 110, 29));
			saveButton.setText("ADD");
			saveButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String msgfromserver = null;
					try 
					{
						long cnum = Long.parseLong(pCnumTextField.getText());
						String description = pDescTextArea.getText();
						double price = Double.parseDouble(pPriceTextField.getText());
						String productname= pNameField.getText();
						if (!description.equals("") &&  !productname.equals("") && cnum > 0)
							if (price > 0)
							{
								ConnectionToServer connectiontoserver = new ConnectionToServer();
								Product product = new Product();
								product.setDescription(description);
								product.setName(productname);
								product.setPrice(price);
								product.setCategoryID(cnum);
								msgfromserver = (String)connectiontoserver.addProduct(product);
								if (msgfromserver != null)
								{
									if (msgfromserver.equals("OK"))
										JOptionPane.showMessageDialog(null, "The product successfully saved");
									if (msgfromserver.equals("IlegalCategorynumber"))
										JOptionPane.showMessageDialog(null, "ERROR: Ilegal Category number");								
								}
								else
									JOptionPane.showMessageDialog(null, "FATAL ERROR");
							}
							else
								JOptionPane.showMessageDialog(null, "Ilegal price");				
						else
						{
							JOptionPane.showMessageDialog(null, "Requested fields not filled");
						}
						
					} catch (ClientNotInitializesException excp)
					{
						JOptionPane.showMessageDialog(null, "ERROR");	
					}
					catch (Exception excp)
					{
						JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
					}	
				}
			});	
					
		}
		return saveButton;
	}
	
	/**
	 * This method initializes removeButton	
	 */	
	private JButton getRemoveButton()
	{
		if (removeButton == null)
		{
			removeButton = new JButton();
			removeButton.setText("Remove");
			removeButton.setBounds(new Rectangle(150, 200, 110, 29));
			removeButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String msgfromserver = null;
					try {
							ConnectionToServer connection_To_server = new ConnectionToServer();
							msgfromserver = (String)connection_To_server.removeProduct(product.getCategoryID());
							if (msgfromserver.equals("OK"))
								JOptionPane.showMessageDialog(null, "The product successfully remove");
							if (msgfromserver.equals("IlegalCategorynumber"))
								JOptionPane.showMessageDialog(null, "ERROR");					
						} catch (ClientNotInitializesException e1)
						{
							JOptionPane.showMessageDialog(null, "FATAL ERROR");
						}
				}
			});			
	  }
		return removeButton;				
	}
	
	/**
	 * This method load product to form
	 * @param product
	 */
	public void loadProduct(Product product)
	{
		this.product = product;
		pDescTextArea.setText(this.product.getDescription());
		pPriceTextField.setText(String.valueOf(this.product.getPrice()));
		pNameField.setText(this.product.getName());
		pCnumTextField.setText(String.valueOf(this.product.getCategoryID()));
	}
	
	/**
	 * This method initializes updateButton	
	 */	
	private JButton getUpdateButton()
	{
		if (updateButton == null)
		{
			updateButton = new JButton();
			updateButton.setText("Update");
			updateButton.setBounds(new Rectangle(150, 200, 110, 29));
			updateButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					try 
					{
						String description = pDescTextArea.getText();
						double price = Double.parseDouble(pPriceTextField.getText());
						String productname= pNameField.getText();
						if (!description.equals("") &&  !productname.equals(""))
							if (price > 0)
							{
								product.setDescription(description);
								product.setName(productname);
								product.setPrice(price);
								String msgfromserver = product.updateProduct();
								if (msgfromserver != null)
								{
									if (msgfromserver.equals("OK"))
										JOptionPane.showMessageDialog(null, "The product successfully updated");
									if (msgfromserver.equals("IlegalCategorynumber"))
										JOptionPane.showMessageDialog(null, "ERROR: Ilegal Category number");								
								}
								else
									JOptionPane.showMessageDialog(null, "FATAL ERROR");
							}
							else
								JOptionPane.showMessageDialog(null, "Ilegal price");				
						else
						{
							JOptionPane.showMessageDialog(null, "Requested fields not filled");
						}
					
					} catch (Exception excp)
					{
						JOptionPane.showMessageDialog(null, "ILEGAL INPUT");	
					}	
				}
		});
	  }
		return updateButton;				
	}
	
	/**
	 * This method initializes cancelButton
	 */	
	public JButton getcancelButton()
	{
		if (cancelButton == null)
		{
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(new Rectangle(15, 200, 100, 29));
		}	
		return cancelButton;				
	}

	/**
	 * This method add Save Button to product form
	 */
	public void addSaveButton()
	{
		this.add(getSaveButton(),null);
	}
	
	/**
	 * This method add Save Button to product form
	 */
	public void addUpdateButton()
	{
		this.add(getUpdateButton(),null);
		pCnumTextField.setEditable(false);	
	}
	
	/**
	 * This method add Save Button to product form
	 */
	public void addRemoveButton()
	{
		this.add(getRemoveButton(),null);
		pCnumTextField.setEditable(false);	
		pNameField.setEditable(false);	
		pPriceTextField.setEditable(false);	
		pDescTextArea.setEditable(false);	
	}
	
}
